package com.uff.hmstpa.model.persistence.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;

import com.uff.hmstpa.model.domain.Healthplan;
import com.uff.hmstpa.model.persistence.AbstractModelDAO;
import com.uff.hmstpa.model.persistence.HealthplanDAO;

public class HealthplanDAOImpl extends AbstractModelDAO implements HealthplanDAO {

	private static HealthplanDAOImpl SOLE_INSTANCE;

	private HealthplanDAOImpl() {
		super();
	}

	public static HealthplanDAO getInstance() {
		if (SOLE_INSTANCE == null) {
			SOLE_INSTANCE = new HealthplanDAOImpl();
		}
		return SOLE_INSTANCE;
	}

	public Healthplan retrieve(int code) {
		String sql = "SELECT * FROM healthplan WHERE code = ?";
		Object[] params = new Object[]{code};
		return jdbcTemplate.queryForObject(sql, params, new HealthplanMapper());
	}

	public List<Healthplan> load() {
		String sql = "SELECT * FROM healthplan";
		return jdbcTemplate.query(sql, new HealthplanMapper());
	}
	
	private static final class HealthplanMapper implements RowMapper<Healthplan> {

		public Healthplan mapRow(ResultSet rs, int rowNum) throws SQLException {
			Healthplan healthplan = new Healthplan();
			healthplan.setCode(rs.getInt("code"));
			healthplan.setName(rs.getString("name"));
			healthplan.setPrice(rs.getDouble("price"));
			return healthplan;
		}
	}

}
